<?php
namespace app\admin\model;
use think\Model;
use think\Loader;
use location;
class Admin extends Model
{   
	//后台管理用户列表显示
	public function index(){
		$all = input('get.all');
		$key = trim(input('get.key','','htmlspecialchars'));
		if($all == 1){	
	      $data = db('admin')->select();
		}else{
			 $data = db('admin')->where('user_name','like',"%".$key."%")->order('admin_id asc')->paginate(15);
		}
	    return $data;
	}
    //后台管理员用户注册  
	public function add(){
		$data['user_name'] = input('post.user_name');
		if(db('admin')->field('user_name')->where($data)->count()){
            //用户已经存在
	         return ['code'=>0,'msg'=>'用户已经存在'];
        }
		$data['password'] = md5(md5(input('post.password')));
		$data['create_time'] = date('Y-m-d H:i:s');
		$data['ip'] = request()->ip();
		$data['group_id'] = input('post.group_id');
		$datas = db('admin')->insertGetId($data);
		if($datas){
			$content = '添加管理员';
        	admin_log($content);
            return ['code'=>1,'msg'=>'添加成功'];
		}else{
			return ['code'=>0,'msg'=>'添加失败'];
		}
	}

	//管理员用户信息修改
	public function edit(){
        $data = input('post.');
        $validate = Loader::validate('Admin');
        //验证不通过
        if(!$validate->check($data)){
         return ['valid'=>0,'msg'=>$validate->getError()];
        }else{
            $admin_id = input('admin_id');
            if($data['password']){
            	$data['password'] = md5(md5(input('post.password')));
            }
            $data['update_time'] = date('Y-m-d H:i:s');
            $datas = db('admin')->where('admin_id',$admin_id)->setField($data);
            if($datas){ 
               $content = '修改管理员信息列表';
        	   admin_log($content);    
               return ['valid'=>1,'msg'=>'修改成功'];
            }else{
                //修改失败
                return ['valid'=>1,'msg'=>'修改失败'];
            }
        }
    }
}